package com.kitty.game.point;


import com.kitty.game.child.model.ChildInfo;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.rank.model.monitor.PetAttribMonitor;
import com.kitty.game.rank.model.monitor.RoleAttribMonitor;
import com.kitty.game.role.message.ReqAssignAttrib;
import com.kitty.game.role.message.ReqSetRecommendAttrib;
import com.kitty.game.role.message.ReqSetRecommendPolar;
import com.kitty.game.role.message.RespAutoShuxing;
import com.kitty.game.role.message.RespAutoXiangxing;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.FieldUtils;
import com.kitty.game.utils.FormulaUtil;
import com.kitty.common.model.Reason;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.fight.service.CountService;
import com.kitty.game.role.service.RoleService;
import org.apache.mina.core.session.IoSession;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PointController {
    Logger logger = LoggerFactory.getLogger(PointController.class);
    @Autowired
    RoleService roleService;
    @RequestMapping
    public void assignResist(IoSession session, ReqAssignResist reqAssignResist) {
        RoleService roleService = SpringUtils.getBean(RoleService.class);
        Role role = SessionUtils.getRoleBySession(session);
        String[] strings = reqAssignResist.getType().split(";");
        logger.info("宠物加抗性======{}===={}", Json.toJson(reqAssignResist), Json.toJson(strings));
        Pet pet = SpringUtils.getPetService().getPetById(reqAssignResist.getPetId(), role);
        if (pet != null) {
            short kangjin, kangmu, kangshui, kanghuo, kangtu;
            short kangyiwang, kangzhongdu, kangbingdong, kanghunshui, kanghunluan;
            kangjin = Short.parseShort(strings[0]);
            kangmu = Short.parseShort(strings[1]);
            kangshui = Short.parseShort(strings[2]);
            kanghuo = Short.parseShort(strings[3]);
            kangtu = Short.parseShort(strings[4]);

            kangyiwang = Short.parseShort(strings[5]);
            kangzhongdu = Short.parseShort(strings[6]);
            kangbingdong = Short.parseShort(strings[7]);
            kanghunshui = Short.parseShort(strings[8]);
            kanghunluan = Short.parseShort(strings[9]);
            int zongKangxing = roleService.getTotalPoint(pet.getLevel(), false);

            int tempKangjin = pet.getResistMetal() + kangjin;
            int tempKangmu = pet.getResistWood() + kangmu;
            int tempKangshui = pet.getResistWater() + kangshui;
            int tempKanghuo = pet.getResistFire() + kanghuo;
            int tempKangtu = pet.getResistEarth() + kangtu;
            int tempKangyiwang = pet.getResistForgotten() + kangyiwang;
            int tempKangzhongdu = pet.getResistPoison() + kangzhongdu;
            int tempKangbingdong = pet.getResistFrozen() + kangbingdong;
            int tempKanghunshui = pet.getResistSleep() + kanghunshui;
            int tempKanghunluan = pet.getResistConfusion() + kanghunluan;

            if (tempKangjin > 30 || tempKangjin < 0) {
                return;
            }
            if (tempKangmu > 30 || tempKangmu < 0) {
                return;
            }
            if (tempKangshui > 30 || tempKangshui < 0) {
                return;
            }
            if (tempKanghuo > 30 || tempKanghuo < 0) {
                return;
            }
            if (tempKangtu > 30 || tempKangtu < 0) {
                return;
            }
            if (tempKangyiwang > 30 || tempKangyiwang < 0) {
                return;
            }
            if (tempKangzhongdu > 30 || tempKangzhongdu < 0) {
                return;
            }
            if (tempKangbingdong > 30 || tempKangbingdong < 0) {
                return;
            }
            if (tempKanghunshui > 30 || tempKanghunshui < 0) {
                return;
            }
            if (tempKanghunluan > 30 || tempKanghunluan < 0) {
                return;
            }
            int last = zongKangxing - (tempKangjin + tempKangmu + tempKangshui + tempKanghuo + tempKangtu
                    + tempKangyiwang + tempKangzhongdu + tempKangbingdong + tempKanghunshui + tempKanghunluan);
            if (last < 0) {
                return;
            }

            int xidianshu = 0;
            for (String s : strings) {//计算洗点数量
                if (Integer.parseInt(s) < 0) {
                    xidianshu -= Integer.parseInt(s);
                }
            }


            logger.info("洗点数量==={}", xidianshu);

            if (xidianshu >= 1) {
                boolean isGold = false;
                int DBSiver = role.getSiver();
                int DBGold = role.getGold();
                if (DBSiver < xidianshu * 50) {//银元宝不足
                    if (DBGold < xidianshu * 50) {//判断金元宝
                        return;
                    } else {
                        isGold = true;
                    }
                }
                if (isGold) {//扣金元
                    SpringUtils.getRoleService().subtractGold(role, xidianshu * 50, Reason.ASSIGN_RESIST);

                    MessagePusher.pushMessage(session, new RespMsg("你花费了#R" + xidianshu * 50 + "#n金元宝，洗掉了#R" + pet.getName() + "#n的#R" + xidianshu + "#n点抗性点。"));
                } else {//扣银元宝
                    roleService.subtractSiver(role, xidianshu * 50, Reason.ASSIGN_RESIST);
                    MessagePusher.pushMessage(session, new RespMsg("你花费了#R" + xidianshu * 50 + "#n银元宝，洗掉了#R" + pet.getName() + "#n的#R" + xidianshu + "#n点抗性点。"));
                }
                roleService.updateRoleGoldAndSiver(role);
            }


            pet.setResistWood(tempKangmu);
            pet.setResistWater(tempKangshui);
            pet.setResistFire(tempKanghuo);
            pet.setResistEarth(tempKangtu);
            pet.setResistForgotten(tempKangyiwang);
            pet.setResistPoison(tempKangzhongdu);
            pet.setResistFrozen(tempKangbingdong);
            pet.setResistMetal(tempKangjin);
            pet.setResistSleep(tempKanghunshui);
            pet.setResistConfusion(tempKanghunluan);
            pet.setLastResistPoint(last);

            role.save();
            {
                //面板属性
                RespUpdate respUpdate = new RespUpdate();
                List<FiedValue> list = new ArrayList<>();
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 65);  //剩余抗性点
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getLastResistPoint());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 50);  //抗性金
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistMetal());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 51);  //抗性木
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistWood());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 52);  //抗性水
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistWater());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 53);  //抗性火
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistFire());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 54);  //抗性土
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistEarth());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 56);  //抗中毒
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistPoison());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 57);  //抗冰冻
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistFrozen());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 58);  //抗昏睡
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistSleep());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 59);  //抗遗忘
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistForgotten());
                    list.add(fiedValue);
                }
                {
                    FiedValue fiedValue = new FiedValue();
                    fiedValue.setType((short) 60);  //抗混乱
                    fiedValue.setVT((byte) 2);
                    fiedValue.setValue((short) pet.getResistConfusion());
                    list.add(fiedValue);
                }
                respUpdate.setRoleId(pet.getId());
                respUpdate.setList(list);
                MessagePusher.pushMessage(session, respUpdate);

                //刷新抗性
                RespAssignResist respAssignResist = new RespAssignResist();
                respAssignResist.setId(pet.getId());
                MessagePusher.pushMessage(session, respAssignResist);
            }

        }

    }

    /**
     * 预览分配属性
     */
    @RequestMapping
    public void preAssignAttrib(IoSession session, ReqPreAssignAttrib reqPreAssignAttrib) {
        CountService countService = SpringUtils.getBean(CountService.class);
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) return;
        int life = 0;
        int mana = 0;
        int phyPower = 0;
        int magPower = 0;
        int speed = 0;
        int defesne = 0;
        if (reqPreAssignAttrib.getId() == 0) {//代表人物洗点
            if (reqPreAssignAttrib.getType() == 1) {//属性点
                short con = reqPreAssignAttrib.getValue1();
                short wiz = reqPreAssignAttrib.getValue2();
                short str = reqPreAssignAttrib.getValue3();
                short dex = reqPreAssignAttrib.getValue4();
                if (str != 0) {
                    int totalEarth = role.getEarth() + role.getExtraEarth();
                    int totalStr = role.getExtendBox().getBasicAttrib().getStr() + role.getStr() + role.getExtraStr();
                    phyPower = countService.countPhyPower(totalEarth, totalStr + str) - countService.countPhyPower(totalEarth, totalStr);
                }
                if (con != 0) {
                    int totalWood = role.getWood() + role.getExtraWood();
                    int totalCon = role.getExtendBox().getBasicAttrib().getCon() + role.getCon() + role.getExtraCon();
                    int totalWater = role.getWater() + role.getExtraWater();

                    life = countService.countLife(role.getLevel(), totalWood, totalCon + con) - countService.countLife(role.getLevel(), totalWood, totalCon);
                    defesne = countService.countDefense(totalWater, totalCon + con) - countService.countDefense(totalWater, totalCon);
                }
                if (wiz != 0) {
                    int totalWood = role.getWood() + role.getExtraWood();
                    int totalMetal = role.getMetal() + role.getExtraMetal();
                    int totalWiz = role.getExtendBox().getBasicAttrib().getWiz() + role.getWiz() + role.getExtraWiz();
                    magPower = countService.countMagPower(totalMetal, totalWiz + wiz) - countService.countMagPower(totalMetal, totalWiz);
                    mana = new Double(countService.countMana(totalWood, role.getWiz() + wiz, role.getLevel()) - countService.countMana(totalWood, role.getWiz(), role.getLevel())).intValue();
                }
                if (dex != 0) {
                    int totalFire = role.getFire() + role.getExtraFire();
                    int totalDex = role.getExtendBox().getBasicAttrib().getDex() + role.getDex() + role.getExtraDex();
                    speed = countService.countSpeed(totalFire, totalDex + dex) - countService.countSpeed(totalFire, totalDex);

                }
                ///然后开始计算增加或者减少的属性值
            } else {//相性点
                short metal = reqPreAssignAttrib.getValue1();
                short wood = reqPreAssignAttrib.getValue2();
                short water = reqPreAssignAttrib.getValue3();
                short fire = reqPreAssignAttrib.getValue4();
                short earth = reqPreAssignAttrib.getValue5();

                if (wood != 0) {
                    int totalWood = role.getWood() + role.getExtraWood();
                    int totalCon = role.getExtendBox().getBasicAttrib().getCon() + role.getCon() + role.getExtraCon();
                    life = countService.countLife(role.getLevel(), totalWood + wood, totalCon) - countService.countLife(role.getLevel(), totalWood, totalCon);
                    mana = new Double(countService.countMana(role.getWiz(), role.getWood() + wood, role.getLevel())).intValue() - new Double(countService.countMana(role.getWiz(), role.getWood(), role.getLevel())).intValue();
                }
                if (metal != 0) {
                    int totalMetal = role.getMetal() + role.getExtraMetal();
                    int totalWiz = role.getExtendBox().getBasicAttrib().getWiz() + role.getWiz() + role.getExtraWiz();
                    magPower = countService.countMagPower(totalMetal + metal, totalWiz) - countService.countMagPower(totalMetal, totalWiz);
                }
                if (water != 0) {
                    int totalCon = role.getExtendBox().getBasicAttrib().getCon() + role.getCon() + role.getExtraCon();
                    int totalWater = role.getWater() + role.getExtraWater();
                    defesne = countService.countDefense(totalWater + water, totalCon) - countService.countDefense(totalWater, totalCon);
                }
                if (fire != 0) {
                    int totalFire = role.getFire() + role.getExtraFire();
                    int totalDex = role.getExtendBox().getBasicAttrib().getDex() + role.getDex() + role.getExtraDex();
                    speed = countService.countSpeed(totalFire + fire, totalDex) - countService.countSpeed(totalFire, totalDex);
                }
                if (earth != 0) {
                    int totalEarth = role.getEarth() + role.getExtraEarth();
                    int totalStr = role.getExtendBox().getBasicAttrib().getStr() + role.getStr() + role.getExtraStr();
                    phyPower = countService.countPhyPower(totalEarth + earth, totalStr) - countService.countPhyPower(totalEarth, totalStr);
                }
            }
        } else {//这里写宠物洗点
            short con = reqPreAssignAttrib.getValue1();
            short wiz = reqPreAssignAttrib.getValue2();
            short str = reqPreAssignAttrib.getValue3();
            short dex = reqPreAssignAttrib.getValue4();

            Pet pet = role.getPetBox().getPetByPetId(reqPreAssignAttrib.getId());
            if (pet == null) {
                return;
            }
            if (pet.getId() == reqPreAssignAttrib.getId()) { //如果ID跟请求的宠物ID相同 则往下执行
                if (con != 0) {
                    defesne = countService.countPetDefense(pet.getLevel(), pet.getCon() + con + pet.getBasicAttrib().getCon(), pet.getTotalLifeEffect())
                            - countService.countPetDefense(pet.getLevel(), pet.getCon() + pet.getBasicAttrib().getCon(), pet.getTotalLifeEffect());
                    life = countService.countPetLife(pet.getLevel(), pet.getCon() + con + pet.getBasicAttrib().getCon(), pet.getLifeEffect())
                            - countService.countPetLife(pet.getLevel(), pet.getCon() + pet.getBasicAttrib().getCon(), pet.getLifeEffect());
                }
                if (wiz != 0) {
                    magPower = countService.countPetMagPower(pet.getLevel(), pet.getWiz() + wiz + pet.getBasicAttrib().getWiz(), pet.getTotalMagEffect())
                            - countService.countPetMagPower(pet.getLevel(), pet.getWiz() + pet.getBasicAttrib().getWiz(), pet.getTotalMagEffect());
                    mana = countService.countPetMana(pet.getLevel(), pet.getWiz() + wiz + pet.getBasicAttrib().getWiz(), pet.getTotalManaEffect())
                            - countService.countPetMana(pet.getLevel(), pet.getWiz() + pet.getBasicAttrib().getWiz(), pet.getTotalManaEffect());
                }
                if (str != 0) {
                    phyPower = countService.countPetPhyPower(pet.getLevel(), pet.getStr() + str + pet.getBasicAttrib().getStr(), pet.getTotalPhyEffect())
                            - countService.countPetPhyPower(pet.getLevel(), pet.getStr() + pet.getBasicAttrib().getStr(), pet.getTotalPhyEffect());
                }
                if (dex != 0) {
                    speed = countService.countPetSpeed(pet.getLevel(), pet.getDex() + pet.getBasicAttrib().getDex() + dex, pet.getTotalSpeedEffect())
                            - countService.countPetSpeed(pet.getLevel(), pet.getDex() + pet.getBasicAttrib().getDex(), pet.getTotalSpeedEffect());
                }
            }
        }

        RespPreAssignAttrib respPreAssignAttrib = new RespPreAssignAttrib();
        respPreAssignAttrib.setType(reqPreAssignAttrib.getType());       //1 属性点 2 相性点
        respPreAssignAttrib.setId(reqPreAssignAttrib.getId());
        respPreAssignAttrib.setLife_plus(0);
        respPreAssignAttrib.setMax_life_plus(life);      //气血
        respPreAssignAttrib.setMana_plus(0);
        respPreAssignAttrib.setMax_mana_plus(mana);       //法力
        respPreAssignAttrib.setPhy_power_plus(phyPower);    //物伤
        respPreAssignAttrib.setMag_power_plus(magPower);    //法伤
        respPreAssignAttrib.setSpeed_plus(speed);       //速度
        respPreAssignAttrib.setDef_plus(defesne);     //防御
        MessagePusher.pushMessage(session, respPreAssignAttrib);


        List<FiedValue> values = FieldUtils.roleShuxin(role);
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(role.getRoleId());
        respUpdate.setList(values);
        MessagePusher.pushMessage(role, respUpdate);
    }


    /**
     * 确定分配属性
     */
    @RequestMapping
    public void assignAttrib(IoSession session, ReqAssignAttrib reqAssignAttrib) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) return;
        RoleService roleService = SpringUtils.getBean(RoleService.class);
        PetService petService = SpringUtils.getBean(PetService.class);
        byte type = reqAssignAttrib.getType();
        int id = reqAssignAttrib.getId();
        if (id >= 1) {
            //宠物加点
            Pet pet = role.getPetBox().getPetByPetId(id);
            PetAttribMonitor petAttribMonitor = new PetAttribMonitor(role, pet);
            logger.info("宠物加点==={}", pet.getName());
            short con = reqAssignAttrib.getValue1();
            short wiz = reqAssignAttrib.getValue2();
            short str = reqAssignAttrib.getValue3();
            short dex = reqAssignAttrib.getValue4();

            int tempDex = pet.getDex() + dex;
            int tempWiz = pet.getWiz() + wiz;
            int tempStr = pet.getStr() + str;
            int tempCon = pet.getCon() + con;
            int attr = roleService.getTotalPoint(pet.getLevel(), true);
            attr += pet.getAddattrNumber();
            int last = attr - (tempDex + tempWiz + tempStr + tempCon);

            if (tempDex < 0 || tempWiz < 0 || tempStr < 0 || tempCon < 0 || last < 0) {
                return;
            }


            List<Short> nums = Arrays.asList(con, str, wiz, dex);
            short xidianNum = 0;
            for (Short num : nums) {
                if (num < 0) {
                    xidianNum -= num;
                }
            }
            if (xidianNum >= 1) {
                int needGold = (int) FormulaUtil.getAttribCost(pet.getLevel(), xidianNum);

                logger.error("计算的洗点价格=={}", needGold);
                boolean isGold = false;
                if (role.getSiver() < needGold) {//银元宝不足
                    if (role.getGold() < needGold) {//判断金元宝
                        MessagePusher.pushMessage(role, new RespMsg("你的金元宝不足。"));
                        return;
                    } else {
                        isGold = true;
                    }
                }
                cost(session, role, roleService, xidianNum, needGold, isGold);
            }

            pet.setDex((short) tempDex);
            pet.setWiz((short) tempWiz);
            pet.setStr((short) tempStr);
            pet.setCon((short) tempCon);
            pet.setBasicAttrib();
            pet.setLastResistPoint(roleService.getLastPolarPoint(pet));
            pet.setLastAtrrib(roleService.getLastAttributePoint(pet));

            petService.refreshPetInfo(role, pet);

            //面板属性
            RespUpdate respUpdate = new RespUpdate();
            List<FiedValue> list = new ArrayList<>();
            {
                FiedValue fiedValue = new FiedValue();
                fiedValue.setType((short) 16);  //剩余属性
                fiedValue.setVT((byte) 2);
                fiedValue.setValue(last);
                list.add(fiedValue);
            }
            {
                FiedValue fiedValue = new FiedValue();
                fiedValue.setType((short) 5);  //体质
                fiedValue.setVT((byte) 2);
                fiedValue.setValue(pet.getCon() + pet.getBasicAttrib().getCon());
                list.add(fiedValue);
            }
            {
                FiedValue fiedValue = new FiedValue();
                fiedValue.setType((short) 9);  //灵力
                fiedValue.setVT((byte) 2);
                fiedValue.setValue(pet.getWiz() + pet.getBasicAttrib().getWiz());
                list.add(fiedValue);
            }
            {
                FiedValue fiedValue = new FiedValue();
                fiedValue.setType((short) 2);  //力量
                fiedValue.setVT((byte) 2);
                fiedValue.setValue(pet.getStr() + pet.getBasicAttrib().getStr());
                list.add(fiedValue);
            }
            {
                FiedValue fiedValue = new FiedValue();
                fiedValue.setType((short) 13);  //敏捷
                fiedValue.setVT((byte) 2);
                fiedValue.setValue(pet.getDex() + pet.getBasicAttrib().getDex());
                list.add(fiedValue);
            }
            respUpdate.setRoleId(id);
            respUpdate.setList(list);
            MessagePusher.pushMessage(role, respUpdate);

            petAttribMonitor.end();
        } else if (id == 0) {//人物加点
            RoleAttribMonitor roleAttribMonitor = new RoleAttribMonitor(role);

            if (type == 1) {///属性
                short con = reqAssignAttrib.getValue1();
                short wiz = reqAssignAttrib.getValue2();
                short str = reqAssignAttrib.getValue3();
                short dex = reqAssignAttrib.getValue4();

                int tempDex = role.getDex() + dex;
                int tempWiz = role.getWiz() + wiz;
                int tempStr = role.getStr() + str;
                int tempCon = role.getCon() + con;

                int last = roleService.getTotalPoint(role.getLevel(), true) + roleService.getNeiDanAttribPoint(role) - (tempDex + tempWiz + tempStr + tempCon);
                last+=roleService.getChildAttribPoint(role);

                if (tempDex < 0 || tempWiz < 0 || tempStr < 0 || tempCon < 0 || last < 0) {
                    return;
                }

                List<Short> nums = Arrays.asList(con, str, wiz, dex);
                short xidianNum = 0;
                for (Short num : nums) {
                    if (num < 0) {
                        xidianNum -= num;
                    }
                }
                if (xidianNum >= 1) {
                    int needGold = (int) FormulaUtil.getAttribCost(role.getLevel(), xidianNum);
                    boolean isGold = false;
                    if (role.getSiver() < needGold) {//银元宝不足
                        if (role.getGold() < needGold) {//判断金元宝
                            MessagePusher.pushMessage(role, new RespMsg("你的元宝不足。"));
                            return;
                        } else {
                            isGold = true;
                        }
                    }
                    cost(session, role, roleService, xidianNum, needGold, isGold);
                }
                role.setDex((short) tempDex);
                role.setWiz((short) tempWiz);
                role.setStr((short) tempStr);
                role.setCon((short) tempCon);
                role.setBasicAttrib();

                role.setLastAttrib(roleService.getLastAttributePoint(role));
            } else if (type == 2) {//相性
                ChildInfo childInfo =role.getChildInfo();
                int maxPolar =childInfo.getMaxPolarExtra()+30;
                short metal = reqAssignAttrib.getValue1();
                short wood = reqAssignAttrib.getValue2();
                short water = reqAssignAttrib.getValue3();
                short fire = reqAssignAttrib.getValue4();
                short earth = reqAssignAttrib.getValue5();
                short totalPolar = (short)(roleService.getTotalPoint(role.getLevel(), false)+roleService.getNeiDanPolarPoint(role));

                int tempMetal = role.getMetal() + metal;
                int tempWood = role.getWood() + wood;
                int tempWater = role.getWater() + water;
                int tempFire = role.getFire() + fire;
                int tempEarth = role.getEarth() + earth;

                if (tempMetal < 0 || tempMetal > maxPolar) {
                    return;
                }
                if (tempWood < 0 || tempWood > maxPolar) {
                    return;
                }
                if (tempWater < 0 || tempWater > maxPolar) {
                    return;
                }
                if (tempFire < 0 || tempFire > maxPolar) {
                    return;
                }
                if (tempEarth < 0 || tempEarth > maxPolar) {
                    return;
                }
                int last = totalPolar - (tempMetal + tempWood + tempWater + tempFire + tempEarth);
                if (last < 0) {
                    return;
                }

                //计算洗相性价格
                int xidianNum = 0;
                Short[] nums = {metal, wood, water, fire, earth};
                for (Short num : nums) {
                    if (num < 0) {
                        xidianNum -= num;
                    }
                }
                logger.info("洗相性点数-----{}", xidianNum);
                if (xidianNum >= 1) {
                    boolean isGold = false;
                    if (role.getSiver() < xidianNum * 164) {//银元宝不足
                        if (role.getGold() < xidianNum * 164) {//判断金元宝
                            MessagePusher.pushMessage(role, new RespMsg("你的元宝不足。"));
                            return;
                        } else {
                            isGold = true;
                        }
                    }
                    if (isGold) {//扣金元宝
                        SpringUtils.getRoleService().subtractGold(role, xidianNum * 164, Reason.ASSIGN_RESIST);
                        MessagePusher.pushMessage(role, new RespMsg("你花费了#R" + xidianNum * 164 + "#n金元宝,洗掉了#R" + xidianNum + "#n点相性点。"));
                    } else {//扣银元宝
                        roleService.subtractSiver(role, xidianNum * 164, Reason.ASSIGN_RESIST);
                        MessagePusher.pushMessage(role, new RespMsg("你花费了#R" + xidianNum * 164 + "#n银元宝,洗掉了#R" + xidianNum + "#n点相性点。"));
                    }
                    roleService.updateRoleGoldAndSiver(role);
                }

                role.setMetal((short) tempMetal);
                role.setWood((short) tempWood);
                role.setWater((short) tempWater);
                role.setFire((short) tempFire);
                role.setEarth((short) tempEarth);
                role.setLastPolar(roleService.getLastPolarPoint(role));
            }
            //发送增益
            SpringUtils.getBean(RoleService.class).sendExtraAttribute(role);
            roleAttribMonitor.end();

        }
        role.save();
    }

    private void cost(IoSession session, Role role, RoleService roleService, short xidianNum, int needGold, boolean isGold) {
        if (isGold) {//扣金元宝
            SpringUtils.getRoleService().subtractGold(role, needGold, Reason.ASSIGN_RESIST);
            MessagePusher.pushMessage(session, new RespMsg("你花费了#R" + needGold + "#n金元宝,洗掉了#R" + xidianNum + "#n点属性点。"));
        } else {//扣银元宝
            roleService.subtractSiver(role, needGold, Reason.ASSIGN_RESIST);
            MessagePusher.pushMessage(session, new RespMsg("你花费了#R" + needGold + "#n银元宝,洗掉了#R" + xidianNum + "#n点属性点。"));
        }
        roleService.updateRoleGoldAndSiver(role);
    }

    /**
     * 设置自动加属性
     *
     * @param session
     * @param reqSetRecommendAttrib
     */
    @RequestMapping
    public void autoShuxing(IoSession session, ReqSetRecommendAttrib reqSetRecommendAttrib) {
        int id = reqSetRecommendAttrib.getId();
        //需要取出玩家请求的数据并保存
        Role role = SessionUtils.getRoleBySession(session);
        Map<String, Byte> nutMap = new HashMap<>();
        nutMap.put("con", reqSetRecommendAttrib.getTizhi());
        nutMap.put("str", reqSetRecommendAttrib.getLiliang());
        nutMap.put("dex", reqSetRecommendAttrib.getMinjie());
        nutMap.put("wiz", reqSetRecommendAttrib.getLingli());
        nutMap.put("status", reqSetRecommendAttrib.getStatus());
        nutMap.put("type", reqSetRecommendAttrib.getType());
        if ((reqSetRecommendAttrib.getTizhi() + reqSetRecommendAttrib.getLiliang() + reqSetRecommendAttrib.getMinjie() + reqSetRecommendAttrib.getLingli()) > 4) {
            return;
        }
        if ((reqSetRecommendAttrib.getTizhi() + reqSetRecommendAttrib.getLiliang() + reqSetRecommendAttrib.getMinjie() + reqSetRecommendAttrib.getLingli()) <= 0) {
            return;
        }
        if (id >= 1) {
            //宠物自动加点
            Pet pet = role.getPetBox().getPetByPetId(id);
            pet.setAutoAtrrib(nutMap);
        } else {
            //人物自动加点
            role.getExtendBox().setAutoAttrib(nutMap);
        }
        role.save();
        RespAutoShuxing respAutoShuxing = new RespAutoShuxing();
        respAutoShuxing.setId(id);
        respAutoShuxing.setLiliang(reqSetRecommendAttrib.getLiliang());
        respAutoShuxing.setLingli(reqSetRecommendAttrib.getLingli());
        respAutoShuxing.setMinjie(reqSetRecommendAttrib.getMinjie());
        respAutoShuxing.setTizhi(reqSetRecommendAttrib.getTizhi());
        respAutoShuxing.setStatus(reqSetRecommendAttrib.getStatus());
        respAutoShuxing.setType(reqSetRecommendAttrib.getType());
        MessagePusher.pushMessage(session, respAutoShuxing);
    }

    /**
     * 设置自动加相性
     *
     * @param session
     * @param reqSetRecommendPolar
     */
    @RequestMapping
    public void autoXiangxing(IoSession session, ReqSetRecommendPolar reqSetRecommendPolar) {
        Role role = SessionUtils.getRoleBySession(session);
        int id = reqSetRecommendPolar.getId();
        //需要取出玩家请求的数据并保存
        if (id >= 1) {
            //宠物自动加点??
        } else {
            Map<String, Byte> nutMap = role.getExtendBox().getAutoPolar();
            nutMap.put("one", reqSetRecommendPolar.getOne());
            nutMap.put("two", reqSetRecommendPolar.getTwo());
            nutMap.put("there", reqSetRecommendPolar.getThere());
            nutMap.put("status", reqSetRecommendPolar.getStatus());
            nutMap.put("type", reqSetRecommendPolar.getType());
            role.save();
        }

        RespAutoXiangxing respAutoXiangxing = new RespAutoXiangxing();
        respAutoXiangxing.setId(id);
        respAutoXiangxing.setOne(reqSetRecommendPolar.getOne());
        respAutoXiangxing.setTwo(reqSetRecommendPolar.getTwo());
        respAutoXiangxing.setThere(reqSetRecommendPolar.getThere());
        respAutoXiangxing.setStatus(reqSetRecommendPolar.getStatus());
        respAutoXiangxing.setType(reqSetRecommendPolar.getType());
        MessagePusher.pushMessage(session, respAutoXiangxing);
    }

}
